﻿using System;
using System.ComponentModel;
using System.Data;
using Newtonsoft.Json;

namespace Smatime.Web.Models
{
    public class CompanyStaffTimeTable : BaseEntity
    {
        public CompanyStaffTimeTable()
        {
            CompanyStaffId = -1;
            StampDate = new DateTime();
            UpdateTime = null;
            Remark = null;
            CheckInTime = null;
            CheckOutTime = null;
            GoOutTime = null;
            TurnBackTime = null;
            StartBreakTime = null;
            EndBreakTime = null;
            RowStatus = 1;
        }

        public long CompanyStaffId { get; set; }
        public DateTime StampDate { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public DateTime? UpdateTime { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore, DefaultValueHandling = DefaultValueHandling.Ignore)]
        [DefaultValue("")]
        public string Remark { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public TimeSpan? CheckInTime { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public TimeSpan? CheckOutTime { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public TimeSpan? GoOutTime { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public TimeSpan? TurnBackTime { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public TimeSpan? StartBreakTime { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public TimeSpan? EndBreakTime { get; set; }

        public long StoreId { get; set; }
        public override BaseEntity FromDataRow(DataRow row)
        {
            Id = (long)row["Id"];
            if (Id < 0)
            {
                Id = -1;
            }
            CompanyStaffId = (long)row["CompanyStaffId"];
            StampDate = (DateTime)row["StampDate"];
            UpdateTime = row["UpdateTime"].ToDateTime();
            Remark = row["Remark"].ToString();
            CheckInTime = row["CheckIn"].ToTimeSpan();
            CheckOutTime = row["CheckOut"].ToTimeSpan();
            GoOutTime = row["GoOut"].ToTimeSpan();
            TurnBackTime = row["TurnBack"].ToTimeSpan();
            StartBreakTime = row["StartBreakTime"].ToTimeSpan();
            EndBreakTime = row["EndBreakTime"].ToTimeSpan();
            StoreId = (long)row["StoreId"];
            return base.FromDataRow(row);
        }

        public override void ToDataRow(DataRow row)
        {
            if (RowStatus == 0)
            {
                row["RowStatus"] = 0;
            }
            else
            {
                row["RowStatus"] = 1;
            }
            if (CompanyStaffId != -1)
            {
                row["CompanyStaffId"] = CompanyStaffId;
            }
            row["StampDate"] = StampDate;
            row["StoreId"] = StoreId;
            if (UpdateTime.HasValue)
            {
                row["UpdateTime"] = UpdateTime;
            }
            if (string.IsNullOrEmpty(Remark))
            {
                row["Remark"] = Remark;
            }
            if (CheckInTime.HasValue)
            {
                row["CheckIn"] = CheckInTime;
            }
            if (CheckOutTime.HasValue)
            {
                row["CheckOut"] = CheckOutTime;
            }
            if (GoOutTime.HasValue)
            {
                row["GoOut"] = GoOutTime.ToRowValue();
            }
            if (TurnBackTime.HasValue)
            {
                row["TurnBack"] = TurnBackTime.ToRowValue();
            }
            if (StartBreakTime.HasValue)
            {
                row["StartBreakTime"] = StartBreakTime.ToRowValue();
            }
            if (EndBreakTime != null)
            {
                row["EndBreakTime"] = EndBreakTime.ToRowValue();
            }


            base.ToDataRow(row);
        }
    }
}